
#include "commonpage.h"
#include "ui_commonpage.h"

#include "listitembox.h"

#include <QListWidgetItem>
#include <QMediaPlaylist>

void CommonPage::setMuisListType(PageType type)
{
    this->pageType = type;
}

CommonPage::CommonPage(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::CommonPage)
{
    ui->setupUi(this);

    ui->pageMusicList->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

    connect(ui->pageMusicList,&QListWidget::doubleClicked,this,[=](const QModelIndex& index){
        //鼠标双击后,发射信号告诉MinMusic,播放能在this页面中被双击的歌曲
        emit playMusicByIndex(this,index.row());
    });
}

void CommonPage::addMusicToMysicPage(const MusicList &musicList)
{
    musicOfPage.clear();

    for (auto music : musicList)
    {
        switch (this->pageType)
        {
            case PageType::LIKE_Page:
                if (music.getIsLike())
                {
                    musicOfPage.push_back(music.getMusicId());
                }
                break;
            case LOCAL_Page:
                musicOfPage.push_back(music.getMusicId());
                break;
            case HISTOARY_Page:
                if (music.getIsHistory())
                {
                    musicOfPage.push_back(music.getMusicId());
                }
                break;

        }
    }
}

CommonPage::~CommonPage()
{
    delete ui;
}

void CommonPage::setCommonPageUi(const QString &text, const QString &imagePath)
{
    ui->PageTittle->setText(text);
    ui->musicImageLabel->setPixmap(QPixmap(imagePath));

    ui->musicImageLabel->setScaledContents(true);


}

void CommonPage::reFresh(MusicList &musicList)
{
    ui->pageMusicList->clear();
    this->addMusicToMysicPage(musicList);

    for (auto musicId : musicOfPage)
    {
        auto it = musicList.findMusicByMusicId(musicId);

        if (it != musicList.end())
        {
            //测试
            //将ListBoxItem对象放到pageMusicList
            ListItemBox* listItemBox = new ListItemBox(this);
            listItemBox->setMusicName(it->getMusicName());
            listItemBox->setMusicSinger(it->getMusicSinger());
            listItemBox->setMusicAlbum(it->getMusicAlbum());
            listItemBox->setLikeMusic(it->getIsLike());


            QListWidgetItem* item = new QListWidgetItem(ui->pageMusicList);
            item->setSizeHint(QSize(listItemBox->width(),listItemBox->height()));
            ui->pageMusicList->setItemWidget(item,listItemBox);
            //本质上这里显示的还是自定义控件,这里的item只是起到一个壳子的作用用来显示我们自定义控件的内容,并控制显示的大小,只是起到用来显示的作用
            //设置hover效果可以重写自定义控件的鼠标进入,离开事件,也可以在List Weidget里面的item设置

            connect(listItemBox,&ListItemBox::setIsLike,this,[=](bool IsLike){
                //更新歌曲的状态
                it->setIsLike(IsLike);
                //需要通知QQMusic,让其中的likePage,localPage,recentPage更新页面信息
                emit updateLikeMusic(IsLike,it->getMusicId());
            });
        }

        //触发窗口的重绘
        //update();//将paintEvent事件放到事件循环队列中,没有立马处理
        repaint();  //立马响应paintEvent事件

    }
}

void CommonPage::addMusicToPlayerList(MusicList &musicList, QMediaPlaylist *playList)
{
    for (auto music: musicList)
    {
        switch (this->pageType)
        {
            case LIKE_Page:
            if (music.getIsLike())
            {
                playList->addMedia(music.getMusicUrl());
            }
            break;
        case LOCAL_Page:
             playList->addMedia(music.getMusicUrl());
            break;
        case HISTOARY_Page:
            if (music.getIsHistory())
            {
                playList->addMedia(music.getMusicUrl());
            }
            break;
        default:
            qDebug() << "未知页面";
        }
    }
}

void CommonPage::setMusicImage(QPixmap pixmap)
{
    ui->musicImageLabel->setPixmap(pixmap);
    ui->musicImageLabel->setScaledContents(true);
}

QString CommonPage::getMusicByIndex(int index)
{
    if (index >= musicOfPage.size())
    {
        qDebug() << "无此歌曲";
        return "";
    }
    return musicOfPage[index];
}

void CommonPage::on_playAllBtn_clicked()
{
    emit playAll(this->pageType);
}
